package com.css.fxfzysx.modules.vaomeFacilities.repository.impl;

import com.css.fxfzysx.modules.vaomeFacilities.entity.CipVulnerabilityEntity;
import com.css.fxfzysx.modules.vaomeFacilities.entity.CipvMatrixEntity;
import com.css.fxfzysx.modules.vaomeFacilities.repository.VmeFacilitiesRepository;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;

@Repository
public class VmeFacilitiesRepositoryImpl implements VmeFacilitiesRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveData(CipvMatrixEntity it) {
        jdbcTemplate.update("insert into czt_important_project_vulner_matrix(cipv_id,intensity,basic_intact,slight_damage,moderate_damage,badly_damaged,collapse,create_user," +
                "create_time,del_flag,cipv_code,cipv_type,cipv_type1,fortification,years,major_project_category,user_name,province) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",it.getCipvId(),it.getIntensity(),it.getBasicIntact(),it.getSlightDamage(), it.getModerateDamage(),
                it.getBadlyDamaged(),it.getCollapse(),it.getCreateUser(),it.getCreateTime(),it.getDelFlag(),it.getCipvCode(),it.getCipvType(),it.getCipvType1(),it.getFortification(),it.getYears(),it.getMajorProjectCategory(),it.getUserName(),it.getProvince());
    }

    @Override
    public void updateData(CipvMatrixEntity it) {
        String sql = "update czt_important_project_vulner_matrix set basic_intact= :basicIntact,slight_damage= :slightDamage,moderate_damage= :moderateDamage,badly_damaged= :badlyDamaged," +
                "collapse= :collapse,update_user= :updateUser,update_time= :updateTime,cipv_type= :cipvType,fortification= :fortification,years= :years,major_project_category= :majorProjectCategory where cipv_id= '"+it.getCipvId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public HashMap<String, Object> queryCipVulnerability(CipvMatrixEntity cipvMatrixEntity, int page, int size) {
        StringBuilder sql = new StringBuilder("select * from czt_important_project_vulner_matrix n where exists( select 1 from czt_important_project_vulner_matrix where del_flag='0'");
        if(cipvMatrixEntity.getRole().equals("0")){
            sql.append(" and create_user ='" + PlatformSessionUtils.getUserId() + "'");
        }
        if(PlatformObjectUtils.isNotEmpty(cipvMatrixEntity.getMajorProjectCategory())){
            sql.append(" and major_project_category ='" + cipvMatrixEntity.getMajorProjectCategory() + "'");
        }
        if(PlatformObjectUtils.isNotEmpty(cipvMatrixEntity.getFortification())){
            sql.append(" and fortification ='" + cipvMatrixEntity.getFortification() + "'");
        }
        if(PlatformObjectUtils.isNotEmpty(cipvMatrixEntity.getYears())){
            if(cipvMatrixEntity.getYears().equals("1")){
                sql.append(" and years = '1978年以前' ");
            }else if(cipvMatrixEntity.getYears().equals("2")){
                sql.append(" and years  = '1978-1989年' ");
            }else if(cipvMatrixEntity.getYears().equals("3")){
                sql.append(" and years = '1989-2001年' ");
            }else if(cipvMatrixEntity.getYears().equals("4")){
                sql.append(" and years  = '2001年以后' ");
            }
        }
        if (!PlatformObjectUtils.isEmpty(cipvMatrixEntity.getCreateStartTime())) {
            sql.append(" and create_time >= '" + cipvMatrixEntity.getCreateStartTime() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cipvMatrixEntity.getCreateEndTime())) {
            String endTime = cipvMatrixEntity.getCreateEndTime() + " 23:59:59";
            sql.append(" and create_time <='" + endTime+"'");
        }
        if (!PlatformObjectUtils.isEmpty(cipvMatrixEntity.getUserName())) {
            sql.append(" and user_name ='" + cipvMatrixEntity.getUserName()+"'");
        }
        StringBuilder orderSql = new StringBuilder(" group by cipv_code having max(cipv_id) =n.cipv_id) order by create_time desc");
        sql.append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CipVulnerabilityEntity.class)).size();
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(limitSql);
        List<CipVulnerabilityEntity> mtoList = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CipVulnerabilityEntity.class));
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", mtoList);
        return resultMap;
    }

    @Override
    public List<CipvMatrixEntity> getDataByNumber(String number) {
        String sql = "select * from czt_important_project_vulner_matrix where del_flag='0' and cipv_code='"+number+"' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CipvMatrixEntity.class));
    }

    @Override
    public void deleteDataByNumber(String number) {
        String sql = "update czt_important_project_vulner_matrix set del_flag = '1' where cipv_code=?";
        jdbcTemplate.update(sql, number);
    }

    @Override
    public int getDateByProvince(String province) {
        try{
            String sql="select count(cbhd_id) from czt_important_project_vulner_matrix where del_flag= '0' and province= ?";
            int count = jdbcTemplate.queryForObject(sql, Integer.class,province);
            return count;
        } catch (Exception e){
            return 0;
        }
    }
}
